package com.codi.api.controller.message;

import com.codi.api.security.UserUtil;
import com.codi.base.log.annotation.Loggable;
import com.codi.bus.core.domain.User;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.resultModel.MessageCountResult;
import com.codi.bus.core.service.UserMessageService;
import com.codi.bus.core.service.UserService;
import com.codi.bus.core.sys.domain.FundCourse;
import com.codi.bus.core.sys.service.FundCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by asus on 2018/4/13.
 */
@Slf4j
@RestController
@RequestMapping("message")
public class MessageController {
    @Autowired
    private UserMessageService userMessageService;

    @Autowired
    private FundCourseService fundCourseService;

    @Autowired
    private UserService userService;




    /**
     * 获取消息记录数
     * @return
     */
    @GetMapping("count")
    @Loggable
    public BaseResult getMessageCount(){
        Long userId = UserUtil.getLoginUserId();
        Integer all = userMessageService.getCount(userId,-1);
        Integer wallet = userMessageService.getCount(userId,1);
        Integer fund = userMessageService.getCount(userId,2);
        Integer portfolio = userMessageService.getCount(userId,3);

//        Integer system = userMessageService.getCount(userId,0);
        Integer system = userMessageService.getSysMessageCount(userId);
        all = all + system;
        MessageCountResult count = MessageCountResult.builder()
            .all(all)
            .fund(fund)
            .portfolio(portfolio)
            .system(system)
            .wallet(wallet)
            .build();
        return new BaseResult().setResult(count);
    }

    @GetMapping("content")
    @Loggable
    public BaseResult getMessage(Integer msgType, Integer titleType, Pageable pageable){
        Long userId = UserUtil.getLoginUserId();

        return new BaseResult().setResult(userMessageService.getMessage(userId,msgType,titleType,pageable));
    }



    @GetMapping("sys_message_list")
    @Loggable
    public BaseResult getSysMessage(Pageable pageable) {
        Long userId = UserUtil.getLoginUserId();

        User user = userService.getUser(userId);

        if (user != null) {
            user.setUserSysMessageCounts(0);
            userService.update(user);
        }

        return new BaseResult().setResult(fundCourseService.queryByCourseType(pageable, FundCourse.TYPE_SYS_MESSAGE));
    }
}
